import { BaseWindow } from "../../../../component/window/BaseWindow";
import { GTextInput } from "../../../../../com/mui/controls/GTextInput";
import { Language } from "../../../../../extend/language/Language";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { KeyCode } from "../../../../../com/gengine/keyBoard/KeyCode";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { SPublicMiniPlayer } from "../../../../../Message/Public/SPublicMiniPlayer";
import { EntityUtil } from "../../../scene/layer/utils/EntityUtil";
import { Cache } from "../../../cache/Cache";
import { MsgManager } from "../../../manager/MsgManager";
import { PlayerMiniInfo } from "../../../model/PlayerMiniInfo";
import { PlayerOpMenuConst } from "../../common/menu/PlayerOpMenuConst";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { CareerConfig } from "../../../resource/CareerConfig";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { FriendStyle } from "../../friends/FriendStyle";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GComboBox } from "../../../../../com/mui/controls/GComboBox";
//class ChatPublicCrossSearchPlayerWindow
    
    export  class ChatPublicCrossSearchPlayerWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this._txInputRoleName = new GTextInput();
            this._txArr = [];
            setSize(304, 235);
            this.titleHeight = 33;
            title = Language.getString(40321);
            this.addListeners();
            return;
        }

        /* internal  */addListeners(): void
        {
            this._btnSearch.addEventListener(flash.events.MouseEvent.CLICK, this.onSearchBtnClick);
            this._txInputRoleName.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, this.keyHandler);
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            var loc1=ObjCreate.createTextField(Language.getString(40322), 35, 47, 54, 20, this);
            this._txInputRoleName = ObjCreate.createGTextInput(89, 47, 115, 20, this);
            this._btnSearch = ObjCreate.createGButton(Language.getString(40323), 213, 46, 47, 22, this);
            this._textTips = ObjCreate.createTextField(Language.getString(40320), 22, 80, 200, 20, this, GlobalStyle.textFormat2);
            this._textTips.visible = false;
            ObjCreate.createBg(17, 71, 269, 126, this);
            this._playerMessagePanel = new Object /* flash.display.Sprite */();
            this._playerMessagePanel.x = 17;
            this._playerMessagePanel.y = 71;
            this._playerMessagePanel.visible = false;
            this.addChild(this._playerMessagePanel);
            return;
        }

        protected onSearchBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.search(this._txInputRoleName.text);
            return;
        }

        /* internal  */keyHandler(arg1: Object /* flash.events.KeyboardEvent */): void
        {
            if (arg1.keyCode == KeyCode.ENTER) 
            {
                this.search(this._txInputRoleName.text);
            }
            return;
        }

        /* internal  */search(arg1: string): void
        {
            if (arg1 != "") 
            {
                this._txInputRoleName.text = arg1;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatPublicCrossSearchPlayerInfo, arg1));
            }
            return;
        }

        public showSearchResults(arg1: Array<any>): void
        {
            var loc1=arg1.length;
            if (loc1 > 0) 
            {
                this._textTips.visible = false;
                this._playerMessagePanel.visible = true;
                this.createPlayerMessage(arg1);
            }
            else 
            {
                this._textTips.visible = true;
                this._playerMessagePanel.visible = false;
            }
            return;
        }

        /* internal  */createPlayerMessage(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=null;
            this._txArr = [];
            while (this._playerMessagePanel.numChildren > 0) 
            {
                this._playerMessagePanel.removeChildAt(0);
            }
            this._dp = null;
            this._dp = new DataProvider();
            var loc4=0;
            while (loc4 < arg1.length) 
            {
                loc1 = arg1[loc4] as SPublicMiniPlayer;
                loc2 = loc4;
                loc3 = "[" + EntityUtil.getProxyName(loc1.entityId) + "]" + loc1.name;
                this._dp.addItem({"id": loc2, "label": loc3, "sInfo": loc1});
                ++loc4;
            }
            this._playerBox = ObjCreate.createGComboBox(52, 5, 205, 25, new DataProvider([]), this._playerMessagePanel);
            this._playerBox.textField.textField.autoSize = flash.text.TextFormatAlign.LEFT;
            this._playerBox.addEventListener(flash.events.Event.CHANGE, this.showPlayerInfo);
            this._talkBtn = ObjCreate.createGButton(Language.getString(40410), 100, 127, 70, 26, this._playerMessagePanel);
            this._talkBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onTlakBtn);
            this._playerBox.dataProvider = this._dp;
            this._playerBox.selectedIndex = 0;
            if (this._playerBox.selectedItem.sInfo) 
            {
                this._info = this._playerBox.selectedItem.sInfo as SPublicMiniPlayer;
                this.showPlayerInfo();
            }
            else 
            {
                this._info = null;
            }
            return;
        }

        protected onTlakBtn(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._info = this._playerBox.selectedItem.sInfo as SPublicMiniPlayer;
            if (EntityUtil.toString(this._info.entityId) == EntityUtil.toString(Cache.instance.role.entityInfo.entityId)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(70802));
                return;
            }
            if (this._info) 
            {
                this._playerMiniInfo = null;
                this._playerMiniInfo = new PlayerMiniInfo(this._info);
                PlayerOpMenuConst.Opearte(PlayerOpMenuConst.ChatPirvate, this._playerMiniInfo);
                this.hide();
            }
            return;
        }

        protected showPlayerInfo(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=null;
            this._info = this._playerBox.selectedItem.sInfo as SPublicMiniPlayer;
            if (this._txArr.length > 0) 
            {
                loc2 = 0;
                while (loc2 < this._txArr.length) 
                {
                    loc1 = this._txArr[loc2];
                    if (loc1 && loc1.parent) 
                    {
                        loc1.parent.removeChild(loc1);
                    }
                    ++loc2;
                }
                this._txArr = [];
            }
            if (this._info) 
            {
                loc3 = ObjCreate.createLabel(Language.getString(40324), 2, 10, 46, 20, flash.text.TextFormatAlign.LEFT, this._playerMessagePanel, false, GlobalStyle.color1Uint);
                this._txArr.push(loc3);
                loc4 = [];
                loc4 = [{"name": Language.getString(40325), "value": this._info.level}, {"name": Language.getString(40326), "value": GameDefConfig.instance.getECamp(this._info.camp)}, {"name": Language.getString(40327), "value": CareerConfig.instance.getInfoByCode(this._info.career).name}, {"name": Language.getString(40328), "value": GameDefConfig.instance.getSex(this._info.sex).name}, {"name": Language.getString(40329), "value": this._info.online ? Language.getString(40330) : Language.getString(40331)}];
                loc5 = 0;
                while (loc5 < loc4.length) 
                {
                    (loc6 = new Object /* flash.text.TextField */()).x = 2 + loc5 % 2 * 137;
                    loc6.y = 36 + Math.floor(loc5 / 2) * 30;
                    loc6.width = 130;
                    loc6.height = 20;
                    loc6.htmlText = HTMLUtil.addColor((loc4[loc5] as Object).name, FriendStyle.color1) + HTMLUtil.addColor((loc4[loc5] as Object).value, this._info.online ? FriendStyle.color2 : GlobalStyle.color3);
                    this._playerMessagePanel.addChild(loc6);
                    this._txArr.push(loc6);
                    ++loc5;
                }
            }
            return;
        }

        /* internal */ /* var */_txInputRoleName: GTextInput;

        /* internal */ /* var */_btnSearch: GButton;

        /* internal */ /* var */_textTips: Object /* flash.text.TextField */;

        /* internal */ /* var */_playerMessagePanel: Object /* flash.display.Sprite */;

        /* internal */ /* var */_info: SPublicMiniPlayer;

        /* internal */ /* var */_playerMiniInfo: PlayerMiniInfo;

        /* internal */ /* var */_playerBox: GComboBox;

        /* internal */ /* var */_talkBtn: GButton;

        /* internal */ /* var */_dp: DataProvider;

        /* internal */ /* var */_txArr: Array<any>;
    }
